package com.cskaoyan.service;

import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.MarketLogExample;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author Yuye Wang
 * @date 2023/5/31 14:54
 * @description TODO
 */
public class LogServiceImpl implements LogService {
    @Override
    public List<MarketLog> list(Integer page, Integer limit, String name, String sort, String order) {
        //开启线程
        TransactionUtil.startTransaction();
        MarketLogMapper marketLogMapper = TransactionUtil.getMapper(MarketLogMapper.class);

        //分页
        PageHelper.startPage(page, limit);

        //构造条件子句
        MarketLogExample example = new MarketLogExample();
        MarketLogExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andAdminLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtil.isEmpty(sort) & !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        //返回响应报文响应封装的对象
        return marketLogMapper.selectByExample(example);
    }

    @Override
    public void log(String admin, String ip, Date addTime, Integer type, String action, Boolean status, String result, String comment) {
        TransactionUtil.startTransaction();
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketLogMapper marketLogMapper = sqlSession.getMapper(MarketLogMapper.class);

        MarketLog marketLog = new MarketLog();
        marketLog.setAdmin(admin);
        marketLog.setIp(ip);
        marketLog.setAddTime(addTime);
        marketLog.setType(type);
        marketLog.setAction(action);
        marketLog.setStatus(status);
        marketLog.setResult(result);
        marketLog.setComment(comment);

        //执行插入
        marketLogMapper.insertSelective(marketLog);
        TransactionUtil.commit();
    }
}
